//
//  HotPostViewController.m
//  唧歪
//
//  Created by ZC on 15/7/8.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "HotPostViewController.h"
#import "HotPostCell.h"
#import "AppDelegate.h"
#import "hotPost.h"
#import "PostDetailsViewController.h"  //帖子详情界面
#import "ImageCompression.h"

#define hotPostURL @"https://app.jiwaipro.com:8888/index.php/JiWai/hotPost"  //热帖
#define browseNumberURL @"https://app.jiwaipro.com:8888/index.php/JiWai/browse"  //浏览数
#define audioPostURL @"https://app.jiwaipro.com:8888/index.php/JiWai/audioPlay"  //语音帖子

@interface HotPostViewController (){
    NSMutableArray *hotPostArray;
    NSMutableArray *array;
    AppDelegate *app;
    NSString *browseNumberStr;
    UIImage *cellImage;
}

@end

@implementation HotPostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"热门帖";
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    hotPostArray=[NSMutableArray array];
    array=[NSMutableArray array];
    
    app.quickOrNot=@"0";
    
    //请求帖子详情数据
    [app isConnectionAvailable];
    if ([app.Existence isEqualToString:@"1"]) {
    }else{
        NSString *poststr =@"";
        [self londingPath:hotPostURL Str:poststr];
        app.Existence = @"0";
    }
    
}
#pragma maek - 请求帖子详情数据
-(void)londingPath:(NSString *)path Str:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        hotPostArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:hotPostArray waitUntilDone:YES];

    }];
}
-(void)refreshData:(NSMutableArray *)sender{
    
    for (NSDictionary *dic in sender) {
        hotPost *hotpost=[[hotPost alloc]init];
        hotpost.releaseTime=dic[@"message_time"];
        hotpost.postContent=dic[@"message_content"];
        hotpost.postComment=dic[@"message_comment"];
        hotpost.messageId = dic[@"message_id"];
        hotpost.messageTag = dic[@"message_tag"];

        [array addObject:hotpost];
    }
    [self.hotPostTableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - 返回tableview的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

#pragma mark - 返回tableview的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
#pragma mark - cell的内容加载
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UINib *nib=[UINib nibWithNibName:@"HotPostCell" bundle:nil];
    [self.hotPostTableView registerNib:nib forCellReuseIdentifier:@"HotPostCell"];
    
    static NSString *cellIdentifier=@"HotPostCell";
    HotPostCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    hotPost *hotP=array[indexPath.row];
    
    cell.hotPostTime.text= [NSString stringWithFormat:@"%@",hotP.releaseTime];
    cell.messageID = [NSString stringWithFormat:@"%@",hotP.messageId];
    cell.messageTag = [NSString stringWithFormat:@"%@",hotP.messageTag];
    
    if ([hotP.messageTag isEqualToString:@"0"]) {
        cell.hotPostContent.hidden = NO;
        cell.audioView.hidden = YES;
        cell.hotPostContent.text=[NSString stringWithFormat:@"%@",hotP.postContent];
        cell.hotPostContent.textColor = [UIColor blackColor];
    }else if ([hotP.messageTag isEqualToString:@"1"]){
        cell.hotPostContent.text = @"[图片帖子]";
        cell.hotPostContent.hidden = NO;
        cell.audioView.hidden = YES;
        cell.hotPostContent.textColor = [UIColor brownColor];
    }else{
        cell.hotPostContent.text = @"[语音消息]";
        cell.hotPostContent.hidden = YES;
        cell.audioView.hidden = NO;
        cell.hotPostContent.textColor = [UIColor colorWithRed:0.0/255.0 green:100.0/255.0 blue:0.0/255.0 alpha:1.0];
    }
    cell.hotPostNumber.text=[NSString stringWithFormat:@"%@",hotP.postComment];
    
    //设置cell的选中时候的颜色
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    hotPost *hotP=array[indexPath.row];
    app.appmessageid = hotP.messageId;
    PostDetailsViewController *postDetail=[self.storyboard instantiateViewControllerWithIdentifier:@"detailVC"];
    [self.navigationController pushViewController:postDetail animated:YES];
    
    NSString *str= [NSString stringWithFormat:@"message_id=%@",hotP.messageId];
    [self loadingPath:browseNumberURL Str:str];

}

-(void)loadingPath:(NSString *)path Str:(NSString *)str{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        browseNumberStr =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData1:) withObject:browseNumberStr waitUntilDone:YES];
    }];
    
}

-(void)refreshData1:(NSString *)str{
    
}




@end
